/**
 * Created by wady on 2017/9/18.
 */
'use strict';
angular.module('backofficeApp.exportReportManager.controller', []).filter('object2Array', function () {
    return function (input) {
        var out = [];
        for (i in input) {
            out.push(input[i]);
        }
        return out;
    }
}).controller('exportReportCtrl', ['$scope', '$http', '$compile', function ($scope, $http, $compile) {

    /** CACHE: This is the heart of the web page
     *
     *  数据结构: { comm1:{date1:{users:10,vusers:5,questions:30,newusers:...},date2:{users:20,...},...}, comm2:{date1:{users:30,...},...}, comm3:{...}, ...}
     *  e.g. $scope.reportingData[comm1][date2][users] = 20
     */

    var graphColumns = {
        users: '总用户数',
        vusers: '总V用户数',
        question: '发布数',
        questionusers: '发布人数',
        replies: '回复数',
        repliesusers: '回复人数'
    }
    $scope.commsInGraph = {}

    $scope.mainRealUsersSwitch = true;

    /*  宝宝树66小区二次改造 */
    /* 生成小区大使信息Excel */
    $scope.exportModeratorManagerDataInfo = function() {


        var startDateSelect = $("#startDateSelect").val();
        if(startDateSelect == '') {
            alert('开始日期不能为空！');
            return;
        }
        var endDateSelect = $("#endDateSelect").val();
        if(endDateSelect == '') {
            alert('结束日期不能为空！');
            return;
        }
        if(startDateSelect > endDateSelect) {
            alert('开始日期不能大于结束日期！');
            return;
        } else {
            var startTemp = new Date(startDateSelect);
            var endTemp = new Date(endDateSelect);
            var sufDays = (endTemp.getTime() - startTemp.getTime()) / 86400000;
            if(sufDays >= 31) {
                alert('只能导出31天及以内的数据！');
                return;
            }
        }
            $(".alert").css("display","block");
        $("#submitModeratorManagerInfoData").attr('disabled',"true"); // 添加disabled属性
        var params = {'startDateSelect': startDateSelect, 'endDateSelect': endDateSelect};
        var config = {timeout: 600000}; // 设置超时时间10分钟,可处理60
        $http.post('http://' + backOfficeHost + '/rest/v1.0/communities/exportModeratorManagerInfoData', params, config).then(
            function successCallback(response) {
                $("#submitModeratorManagerInfoData").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                var data = response.data.data;
                if(data == null) {
                    alert('导出失败!');
                } else {
                    /*  宝宝树66小区二次改造 wangcan 20170922 start */
                    // 如果导出文件行数小于小区大使件数，请稍等片刻后在点击确定按钮!
                    alert('导出【'+ data +'】个小区大使数据成功。');
                    /*  宝宝树66小区二次改造 wangcan 20170922 end */

                    /*  宝宝树66小区二次改造 */
                    window.open('http://' + backOfficeHost + '/rest/v1.0/communities/downLoadModeratorManagerInfoData');
                    /*  宝宝树66小区二次改造 */
                }
            }, function errorCallback(response) {
                $("#submitModeratorManagerInfoData").removeAttr("disabled"); // 移除disabled属性
                $(".alert").css("display","none");
                alert('导出失败!');
            })
    };
}])